layui.extend({
    baseConfig : "/js/pages/baseConfig"
}).use(['form','layer','table','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var id = getUrlParamer( "id");
    var total = Math.abs( parseFloat( getUrlParamer( "total")).toFixed( 2));
    $( "input[name=total_span]").val( total);

    //自动设置金额2的值
    $("input[name=one_span]").on("input propertychange",function(e){
        $("input[name=two_span]").val( total - parseFloat( $("input[name=one_span]").val()).toFixed( 2))
    });

    //自动设置金额1的值
    $("input[name=two_span]").on("input propertychange",function(e){
        $("input[name=one_span]").val( total - parseFloat( $("input[name=two_span]").val()).toFixed( 2))
    });

    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        var moneyOne = parseFloat( $("input[name=one_span]").val()).toFixed( 2);
        var moneyTwo = parseFloat( $("input[name=two_span]").val()).toFixed( 2);

        if( !moneyOne){
            layer.msg( "金额1不能为空");
            return;
        }
        if( !moneyTwo){
            layer.msg( "金额2不能为空");
            return;
        }

        //提交数据
        $.post("/consumer/partition",{
            id : id,
            moneyOne : moneyOne,
            moneyTwo : moneyTwo
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( "拆分成功");
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });
});
